package cn.school.thoughtworks.section3;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PracticeD {
    Map<String, Integer> createUpdatedCollection(List<String> collectionA, Map<String, List<String>> object) {
        //实现练习要求，并改写该行代码。
        Map<String, Integer> collection3 = new HashMap<String, Integer>();
        String pattern = "\\d+";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        for (int i = 0; i < collectionA.size(); i++) {
            // Now create matcher object.
            Matcher m = r.matcher(collectionA.get(i));

            if (m.find()) {
                Integer sum = collection3.get(collectionA.get(i).substring(0, 1));
                collection3.put(collectionA.get(i).substring(0, 1), sum == null ?
                        Integer.valueOf(m.group()) : sum + Integer.valueOf(m.group())
                );
            } else {
                Integer sum = collection3.get(collectionA.get(i));
                collection3.put(collectionA.get(i), sum == null ? 1 : sum + 1);
            }

        }
        for (String s:collection3.keySet()){
           for (String st:object.get("value")){
               if(s.equals(st)){
                   collection3.put(s,collection3.get(s)-collection3.get(s)/3);
               }
           }
        }
        return collection3;
    }
}
